package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;



    //Request URL: http://192.168.37.1:8500/api/user/wxLogin/wxLogin/0d3vbR100SmnBU1JXb300MnMCI3vbR1J

    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable String code){
        Map<String,String> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }
    @GetMapping("/getUserInfo")
    @Login
    public Result getUserInfo(){
        Long userId = AuthContextHolder.getUserId();
        log.info("userId:"+userId);
        UserInfoVo userInfoVo =userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    //http://192.168.37.1:8500/api/user/wxLogin/updateUser

    @Login
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        Long userId = AuthContextHolder.getUserId();
        userInfoService.updateUser(userInfoVo,userId);
        return Result.ok();

    }

}
